package com.investmenttechnologyservices.model;

import java.util.Date;
import java.util.Set;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


public class Position extends EntityHolder {
/*	private Key accountKey;
	private Key symbolKey;
	private Double quantity;
	private Double cost;
	private Double marketValue;
	private Double exposureValue;
	private PositionType positionType;
	private StageType stageType;
	private Date settlementDate;
	private Key tradeKey;
	private Boolean cashPosition;
	private PriceFormat priceFormat;
	private InstrumentType instrumentType;
	private Key originalSymbolKey;
	private Set<String> tags;
*/	
	public Position(Entity entity) {
		super(entity);
	}

	public Position(Key accountKey, Key symbolKey, Double quantity, Double cost, 
			Double marketValue, Double exposureValue, PositionType positionType, 
			StageType stageType, Date settlementDate, Key tradeKey, Boolean cashPosition, 
			PriceFormat priceFormat, InstrumentType instrumentType, Key originalSymbolKey, 
			Set<String> tags) {
		super("position");

		setAccountKey(accountKey);
		setSymbolKey(symbolKey);
		setQuantity(quantity);
		setCost(cost);
		setMarketValue(marketValue);
		setExposureValue(exposureValue);
		setPositionType(positionType);
		setStageType(stageType);
		setSettlementDate(settlementDate);
		setTradeKey(tradeKey);
		setCashPosition(cashPosition);
		setPriceFormat(priceFormat);
		setInstrumentType(instrumentType);
		setOriginalSymbolKey(originalSymbolKey);
		
		setTags(tags);
	}
	
	public Position(Key accountKey, Key symbolKey, Double quantity, PositionType positionType, StageType stageType, Date settlementDate, Key tradeKey,
			Boolean cashPosition, PriceFormat priceFormat, Key originalSymbolKey, InstrumentType instrumentType) {
		this(accountKey, symbolKey, quantity, 0.0, 0.0, 0.0, positionType, stageType, 
				settlementDate, tradeKey, cashPosition, priceFormat, instrumentType, originalSymbolKey, null);
	}

	public void setAccountKey(Key accountKey) {setProperty("accountKey", accountKey);}
	public Key getAccountKey() {return (Key)getProperty("accountKey");}
	public void setSymbolKey(Key symbolKey) {setProperty("symbolKey",symbolKey);}
	public Key getSymbolKey() {return (Key)getProperty("symbolKey");}
	public void setQuantity(Double quantity) {setProperty("quantity",quantity);}
	public Double getQuantity() {return (Double)getProperty("quantity");}
	public void setCost(Double cost) {setProperty("cost",cost);}
	public Double getCost() {return (Double) getProperty("cost");}
	public void setMarketValue(Double marketValue) {setProperty("marketValue",marketValue);}
	public Double getMarketValue() {return (Double) getProperty("marketValue");}
	public void setExposureValue(Double exposureValue) {setProperty("exposureValue",exposureValue);}
	public Double getExposureValue() {return (Double) getProperty("exposureValue");}
	public void setPositionType(PositionType positionType) {setProperty("positionType",positionType);}
	public PositionType getPositionType() {return (PositionType)getProperty("positionType");}
	public void setStageType(StageType stageType) {setProperty("stageType",stageType);}
	public StageType getStageType() {return (StageType)getProperty("stageType");}
	public void setSettlementDate(Date settlementDate) {setProperty("settlementDate",settlementDate);}
	public Date getSettlementDate() {return (Date) getProperty("settlementDate");}
	public void setTradeKey(Key tradeKey) {setProperty("tradeKey",tradeKey);}
	public Key getTradeKey() {return (Key)getProperty("tradeKey");}
	public void setCashPosition(Boolean cashPosition) {setProperty("cashPosition",cashPosition);}
	public Boolean getCashPosition() {return (Boolean)getProperty("cashPosition");}
	public void setPriceFormat(PriceFormat priceFormat) {setProperty("priceFormat",priceFormat);}
	public PriceFormat getPriceFormat() {return (PriceFormat)getProperty("priceFormat");}
	public void setInstrumentType(InstrumentType instrumentType) {setProperty("instrumentType",instrumentType);}
	public InstrumentType getInstrumentType() {return (InstrumentType)getProperty("instrumentType");}
	public void setOriginalSymbolKey(Key originalSymbolKey) {setProperty("originalSymbolKey",originalSymbolKey);}
	public Key getOriginalSymbolKey() {return (Key)getProperty("originalSymbolKey");}
	public void setTags(Set<String> tags) {setProperty("tags",tags);}
	@SuppressWarnings("unchecked")
	public Set<String> getTags(){return (Set<String>)getProperty("tags");}
	
	
	//Functions to support standard DataStoreHelper service
	
	public Key getKey() {
		return KeyFactory.createKey("position", 100L);
	}
	/*
	public Object getProperty(String property) {
		Class<?> c;
		try {
			c = Class.forName(this.getClass().getName());

			String strMethod = "get"+property;
			try {
				Method myMethod = c.getDeclaredMethod(strMethod);
				try {
					Object object = (Object) myMethod.invoke(this);
					return object;
				} catch (IllegalArgumentException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				} catch (IllegalAccessException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				} catch (InvocationTargetException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				}
			} catch (SecurityException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			} catch (NoSuchMethodException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			}  // Works!
			
		} catch (ClassNotFoundException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
			return null;
		}
		return null;
	}
	*/
	
	@Override
	public String toString() {
		return "Position [Account="+getAccountKey().toString() +
				", Symbol=" + getSymbolKey().toString() + 
				", \tQuantity=" + getQuantity().toString() +
				", \tCost=" + getCost().toString() + ", \tMarket Value=" + getMarketValue().toString() +
				", \tExposure Value=" + getExposureValue().toString() +
				", \tPosition type="+getPositionType()+ ", \tStage type="+getStageType()+
				", \tSettlement date="+getSettlementDate()+ ", \tradeKey="+getTradeKey().toString()+
				", \tInstrument type="+getInstrumentType()+ ",\tPxFormat="+getPriceFormat().toString()+
				", \tOriginalSymbolKey="+((getOriginalSymbolKey()==null)?"":getOriginalSymbolKey())+
				((getTags()==null)?"":", \tTags="+getTags().toString())+"]";
	}
		
	public int compareTo(Position comparePosition) {
			 
		Date compareDate = comparePosition.getSettlementDate(); 
		//ascending order
		return this.getSettlementDate().compareTo(compareDate);
 
	}	

	
}

